﻿/**
* Rnd by Grant Skinner. Jan 15, 2008
* Visit www.gskinner.com/blog for documentation, updates and more free code.
*
*
* Copyright (c) 2008 Grant Skinner
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package tools.utils {
	// Utility class provides common random functions.

	/**
	 * 随机函数,可以根据概率返回随机值
	*/
	public class Rnd {
		// static interface:

		/**
		 * 返回一个0-1之间的伪随机数
		*/
		public static function random():Number {
			return Math.random();
		}

		/**
		 * 返回一个区间随机浮点数
		 * @param min 最小值
		 * @param max 最大值
		 * @example
		 * <LISTING>
		 * // float(50); // returns a number between 0-50 exclusive
		 * // float(20,50); // returns a number between 20-50 exclusive
		 * </LISTING>
		*/
		public static function float(min : Number, max : Number = NaN, num : int = -1):Number {
			if (isNaN(max)) {
				max = min;
				min = 0;
			}
			var result : Number;
			result = random() * (max - min) + min;
			if(num > 0) {
				result = int(result * Math.pow(10, num)) / Math.pow(10, num);
			}
			return result;
		}

		/**
		 * 返回一个随机布尔值
		 * @param chance 几率
		 * @example
		 * <LISTING>
		 * // boolean(); // returns true or false (50% chance of true)
		 * // boolean(0.8); // returns true or false (80% chance of true)
		 * </LISTING>
		*/
		public static function boolean(chance : Number = 0.5):Boolean {
			return (random() < chance);
		}

		/**
		 * 返回一个随机符号
		 * @param chance 几率
		 * @example
		 * <LISTING>
		 * // sign(); // returns 1 or -1 (50% chance of 1)
		 * // sign(0.8); // returns 1 or -1 (80% chance of 1)
		 * </LISTING>
		*/
		public static function sign(chance : Number = 0.5):int {
			return (random() < chance) ? 1 : -1;
		}

		/**
		 * 返回在0或1中的随机值
		 * @param chance 几率
		 * @example
		 * <LISTING>
		 * // bit(); // returns 1 or 0 (50% chance of 1)
		 * // bit(0.8); // returns 1 or 0 (80% chance of 1)
		 * </LISTING>
		*/
		public static function bit(chance : Number = 0.5):int {
			return (random() < chance) ? 1 : 0;
		}

		/**
		 * 返回一个区间的随机整数
		 * @param min 最小整数
		 * @param max 最大整数(值域不包含此数)
		 * @example
		 * <LISTING>
		 * // integer(50); // returns an integer between 0-49 inclusive
		 * // integer(20,50); // returns an integer between 20-49 inclusive
		 * </LISTING>
		*/
		public static function integer(min : Number, max : Number = NaN):int {
			if (isNaN(max)) {
				max = min;
				min = 0;
			}
			// Need to use floor instead of bit shift to work properly with negative values:

			return Math.floor(float(min, max));
		}
		
		public static function randomArr(num:int):Array
		{
			var arr:Array = [];
			do {
				var rndNum:int = integer(1,num + 1)
				if (arr.indexOf(rndNum) < 0)
				{
					arr.push(rndNum);
				}
			}while (arr.length < num);
			return arr;
		}
		
		public static function integerdot(m:int):int{
			var a:int = integer(Math.pow(10,m - 1),Math.pow(10,m));
			return a;
		}

		public function Rnd(seed : uint = 0) {
			throw(new Error("the Rnd class cannot be instantiated"));
		}
	}
}